import axios from 'axios'
// import store from '@/store'
import { Message } from 'element-ui'
import store from '@/store'

const service = axios.create({
  baseURL: 'https://likede2-admin.itheima.net/likede',
  timeout: 5000
})

service.interceptors.request.use(
  (config) => {
    if (store.getters.token) {
      config.headers.Authorization = store.getters.token
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
// 响应拦截器
service.interceptors.response.use((response) => {
  const { data, status } = response
  if (data.success) {
    return data
  } else if (status === 200) {
    return data
  } else {
    Message({ type: 'error', message: data.msg })
    return Promise.reject(new Error('未知错误'))
  }
}, async(error) => {
  // error.message
  // Message.error('这个不知道是什么错误，反正就是错了')
  Message.error(error.response?.data?.message || '这个不知道是什么错误，反正就是错了')
  return Promise.reject(error)
})

export default service
